/**
 * gbl 评论部分内容
 *
 */
var post_flag = false;
var post_flag2 = false;

// 监听主评论添加点击事件，添加主评论
function addComment(commodityId) {
    //如果正在提交则直接返回，停止执行
    if (post_flag) return;
    //标记当前状态为正在提交状态
    post_flag = true;
    var text = document.getElementById("js-pl-textarea");
    if (text.value == ''){
        alert("没有输入内容");
    }else {
        var foodId = commodityId;
        var comText = text.value;
        // alert(decodeURI(commodityId));
        // 获取当前用户id
        $.get("user/finduser", {}, function (data) {
            if (data.length > 0) {
                var user = data;
                var comment = {"commodityId": foodId,
                    "commodityComment": comText,
                    "userId": user};
                $.ajax({
                    url: "/comment/addComment",
                    dataType: "json",
                    data: comment,
                    success: function (date) {
                        if (date.code == 1){
                            alert(date.message);
                            var time1 = date.data.commentHours;
                            var d = new Date(time1);
                            var times=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
                            var indexInfo = '\n' +
                                '<li class="post-row">\n' +
                                '     <div class="media">\n' +
                                '          <span><img src=\'images/user1.png\' width=\'40\' height=\'40\'/></span>\n' +
                                '     </div>\n' +
                                '     <div class="bd">\n' +
                                '          <div class="tit">\n' +
                                '               <span >'+ date.data.userId +'</span>\n' +
                                '          </div>\n' +
                                '          <p class="cnt">'+ date.data.commodityComment +'</p>\n' +
                                '          <div class="footer ">\n' +
                                '               <span title="创建时间" class="">'+ times +'</span>\n' +
                                '               <div class="reply"><button class="addreply">回复</button></div>\n' +
                                '               <div class="replyText" hidden>\n' +
                                '                    <input class="inputReply" placeholder="随便写一点">\n' +
                                '                    <div class="issue"><button onclick="getReplyInfo('+ date.data.id +')">发布</button></div>\n' +
                                '               </div>\n' +
                                '          </div>\n' +
                                '          <!-- twoReply下面的内容是二级评论 -->\n' +
                                '          <div class="towReply">\n' +
                                '              <!-- 二级评论模型 -->\n' +
                                '              <ul class="mod-post">' +
                                '              </ul>\n' +
                                '          </div>\n' +
                                '     </div>\n' +
                                '</li>';
                            $('#comment-list').append(indexInfo);
                        }
                        // 提交成功修改会课提交状态
                        post_flag = false
                    }
                })
            }else {
                location.href = "error/403.html"
            }
        });


    }
}


// // 发布按钮事件监听
// function getReplyInfo(comId) {
//     //comId传过来的主评论id
//     var text = $(this).parent().parent().val();
//     alert(text);
//     // 获取当前用户id
//     $.get("user/finduser", {}, function (data) {
//         if (data.length > 0) {
//             var user = data;
//             var comCom = {
//                 "topicId": comId,
//                 "content": comText,
//                 "userId": user
//             };
//         }
//     });
// }



// 评论内容的加载
$(function () {
    // 获取当前的商品id加载对应的评论信息
    var commodityId = getQueryVariable();
    $.ajax({
        url: '/comment/getComment',
        dataType: "json",
        data: "commodityId="+commodityId,
        success: function (date) {
            var html = '';
            date.commentList.map(function (item,index) {
                // 转换时间显示
                var time1 = item.commentHours;
                var d = new Date(time1);
                var times=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
                html+='\n' +
                    '<li class="post-row">\n' +
                    '     <div class="media">\n' +
                    '          <span><img src=\'images/user1.png\' width=\'40\' height=\'40\'/></span>\n' +
                    '     </div>\n' +
                    '     <div class="bd">\n' +
                    '          <div class="tit">\n' +
                    '               <span>'+ item.userId +'</span>\n' +
                    '          </div>\n' +
                    '          <p class="cnt">'+ item.commodityComment +'</p>\n' +
                    '          <div class="footer ">\n' +
                    '               <span title="创建时间" class="">'+ times +'</span>\n' +
                    '               <div class="reply"><button class="addreply">回复</button></div>\n' +
                    '               <div class="replyText" hidden>\n' +
                    '                    <input class="inputReply" placeholder="随便写一点">\n' +
                    '                    <div class="issue"><button class="issuecom" >发布</button></div>\n' +
                    '                    <div id="'+ item.id +'" class="h_comId" hidden>'+ item.id +'</div>' +
                    '               </div>\n' +
                    '          </div>\n' +
                    '          <!-- twoReply下面的内容是二级评论 -->\n' +
                    '          <div class="towReply">\n' +
                    '              <!-- 二级评论模型 -->\n' +
                    '              <ul class="mod-post">';
                if (item.commentComList != null){
                    for (var i=0; i<item.commentComList.length; i++){
                        var time11 = item.commentHours;
                        var d1 = new Date(time11);
                        var times1=d.getFullYear() + '-' + (d1.getMonth() + 1) + '-' + d1.getDate() + ' ' + d1.getHours() + ':' + d1.getMinutes() + ':' + d1.getSeconds();
                        html += '<li class="post-row2">\n' +
                            '        <div class="media">\n' +
                            '            <span><img src=\'images/user1.png\' width=\'30\' height=\'30\' style="margin-top: 5px"/></span>\n' +
                            '        </div>\n' +
                            '        <div class="bd2">\n' +
                            '            <div class="tit">\n' +
                            '                <span>'+ item.commentComList[i].fromUid +'@'+ item.commentComList[i].toTpid +'</span>\n' +
                            '            </div>\n' +
                            '            <p class="cnt">'+ item.commentComList[i].content +'</p>\n' +
                            '            <div class="footer">\n' +
                            '                <span title="创建时间" class="">'+ times1 +'</span>\n' +
                            '                <div class="reply">\n' +
                            '                    <button class="addreply">回复</button>\n' +
                            '                </div>\n' +
                            '                <div class="replyText" hidden>\n' +
                            '                    <input class="inputReply" placeholder="随便写一点">\n' +
                            '                    <div class="issue2"><button class="issuecom">发布</button></div>\n' +
                            '                    <div class="h_comId" hidden>'+ item.id +'</div>' +
                            '                </div>\n' +
                            '            </div>\n' +
                            '         </div>\n' +
                            '      </li>';
                    }
                }
                html += '          </ul>\n' +
                    '          </div>\n' +
                    '     </div>\n' +
                    '</li>';
            })
            $('#comment-list').html(html);
        }
    })
})


// 监听回复点击事件
$('body').on('click','.addreply',function () {
    if ($(this).parent().next('div').is(':hidden')){
        //$(this).parent().next('div')
        $(this).parent().next('div').show();
    } else {
         $(this).parent().next('div').hide();
    }
});


// 监听发布点击事件
$('body').on('click','.issuecom',function () {
    //如果正在提交则直接返回，停止执行
    if (post_flag2) return;
    //标记当前状态为正在提交状态
    post_flag2 = true;
    // 获取主评论ID
    var comId = $(this).parent().next('div').html();
    // 获取评论内容
    var comText = $(this).parent().parent().children('input').val();
    var toUser = $(this).parent().parent().parent().parent().children('.tit').children('span').html();

    $.get("user/finduser", {}, function (data) {
        if (data.length > 0) {
            var user = data;
            var comCom = {
                "topicId": comId,
                "content": comText,
                "fromUid": user,
                "toTpid": toUser,
            };
            $.ajax({
                url: "/comment/addComCom",
                dataType: "json",
                data: comCom,
                success: function (date) {
                    if (date.code == 1){
                        // 数据传输成功，修改状态
                        post_flag2 = false;
                        alert("评论发布成功");
                        var time2 = date.data.comTime;
                        var d = new Date(time2);
                        var times=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
                        var indexInfo = '' +
                            '<li class="post-row2">\n' +
                            '   <div class="media">\n' +
                            '       <span><img src=\'images/user1.png\' width=\'30\' height=\'30\'/></span>\n' +
                            '   </div>\n' +
                            '   <div class="bd2">\n' +
                            '       <div class="tit">\n' +
                            '           <span>'+ date.data.fromUid +'@'+ date.data.toTpid +'</span>\n' +
                            '       </div>\n' +
                            '       <p class="cnt">'+ date.data.content +'</p>\n' +
                            '       <div class="footer">\n' +
                            '           <span title="创建时间" class="">' + times + '</span>\n' +
                            '           <div class="reply">\n' +
                            '               <button class="addreply">回复</button>\n' +
                            '           </div>\n' +
                            '           <div class="replyText" hidden>\n' +
                            '               <input class="inputReply" placeholder="随便写一点">\n' +
                            '               <div class="issue2"><button class="issuecom">发布</button></div>\n' +
                            '               <div hidden>'+ date.data.id +'</div>' +
                            '           </div>\n' +
                            '       </div>\n' +
                            '   </div>\n' +
                            '</li>';
                        $(this).parent().parent().parent().children('.towReply').children('ul').append(indexInfo);
                        window.location.reload();
                    }else {
                        alert("当前繁忙，请稍后")
                    }
                }
            })
        }else{
            location.href = "error/403.html"
        }
    });

});
